﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using WebSiteAdminTool.Common.Interfaces;

namespace WebSiteAdminTool.Web.Services.DTOs
{
    [DataContract]
    public class RoleData : IIdentifiable, IValidatable
    {
        [DataMember]
        public Guid RoleId { get; set; }
        [DataMember]
        public string RoleName { get; set; }
        [DataMember]
        public int UsersInRole { get; set; }
        [DataMember]
        public string Description { get; set; }
        
        //public RoleData(string roleName, int usersInRole, string comment = null)
        //{
        //    this.RoleName = roleName;
        //    this.UsersInRole = usersInRole;
        //    this.Comment = comment; 
        //}

        public RoleData(DataAccess.aspnet_Roles dbRole, int usersInRole)
        {
            this.RoleId = dbRole.RoleId;
            this.RoleName = dbRole.RoleName;
            this.Description = dbRole.Description;
            this.UsersInRole = usersInRole;
        }

        #region IIdentifiable implementation

        [DataMember]
        public object Id
        {
            get { return this.RoleId; }
            set 
            { 
                // emty setter - there's no need to update role name, but datacontract attr still requires a public setter
            }
        }

        #endregion

        #region IValidatable implementation

        public bool Validate()
        {
            if (string.IsNullOrWhiteSpace(RoleName))
                throw new InvalidOperationException("The given role instance is not properly instantiated.");

            return true;
        }

        #endregion
    }
}